package dyy.zc.campuspay.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import dyy.zc.campuspay.R;
import dyy.zc.campuspay.application.MyApplication;
import dyy.zc.campuspay.bean.Account;
import dyy.zc.campuspay.ui.fragment.MineFragment;
import dyy.zc.campuspay.utils.BaseTranslucentActivity;
import dyy.zc.campuspay.utils.BmobUtils;
import dyy.zc.campuspay.utils.ToastUtils;

public class ChangeRealNameActivity extends BaseTranslucentActivity {
    private EditText name;
    private ImageView delete;
    private TextView title;
    private TextView save;
    //private Ac
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_nikename);
        findViewsById();
        bind();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = getIntent();
        if (intent != null) {
            String str = intent.getStringExtra("real");
            String str1 = intent.getStringExtra("title");
            if (str1 != null) {
                title.setText(str1);
            }
            if (str != null) {
                name.setText(str);
            }
        }
    }

    private void bind() {
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name.setText("");
            }
        });
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = name.getText().toString();
                if (str.equals("")) {
                    ToastUtils.shortToast(ChangeRealNameActivity.this, "还没有输入名字哦~~");
                } else {
                    Account account = MyApplication.getInstance().readLoginUser();
                    if (MineFragment.type == 0){
                        account.setNikeName(str);
                    }else {
                        account.setRealName(str);
                    }
                    MyApplication.getInstance().saveUserInfo(account);
                    BmobUtils bmobUtils = new BmobUtils(ChangeRealNameActivity.this);
                    bmobUtils.updateAccountInfo(account);
                    ToastUtils.shortToast(ChangeRealNameActivity.this,"修改成功");
                    setResult(960, ChangeRealNameActivity.this.getIntent().putExtra("name", str));
                    finish();
                }
            }
        });
    }

    private void findViewsById() {
        name = (EditText) findViewById(R.id.changeNikeName_et_nikeName);
        delete = (ImageView) findViewById(R.id.changeNikeName_iv_reset);
        save = (TextView) findViewById(R.id.top_tv_right);
        title = (TextView) findViewById(R.id.top_tv_title);
        save.setVisibility(View.VISIBLE);
        save.setText("保存");
    }
}
